package cn.da.shuai.cool.system.service.service.impl;

import cn.da.shuai.cool.mybatis.service.impl.CoolServiceImpl;
import cn.da.shuai.cool.system.service.mapper.CoolJobLogMapper;
import cn.da.shuai.cool.system.model.entity.job.CoolJobLog;
import cn.da.shuai.cool.system.service.service.ICoolJobLogService;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.List;

/**
 * 定时任务调度日志服务实现类
 */
@Service
public class CoolJobLogServiceImpl extends CoolServiceImpl<CoolJobLogMapper, CoolJobLog> implements ICoolJobLogService {

    @Override
    public List<CoolJobLog> selectJobLogList(CoolJobLog jobLog) {
        return this.list(getWrapper(jobLog));
    }

    @Override
    public Page<CoolJobLog> selectJobLogPage(CoolJobLog jobLog, Page<CoolJobLog> page) {
        return this.page(page, getWrapper(jobLog));
    }

    private LambdaQueryWrapper<CoolJobLog> getWrapper(CoolJobLog jobLog) {
        LambdaQueryWrapper<CoolJobLog> lqw = new LambdaQueryWrapper<>();
        lqw.like(StrUtil.isNotBlank(jobLog.getJobName()), CoolJobLog::getJobName, jobLog.getJobName())
           .eq(StrUtil.isNotBlank(jobLog.getJobGroup()), CoolJobLog::getJobGroup, jobLog.getJobGroup())
           .eq(StrUtil.isNotBlank(jobLog.getStatus()), CoolJobLog::getStatus, jobLog.getStatus())
           .like(StrUtil.isNotBlank(jobLog.getInvokeTarget()), CoolJobLog::getInvokeTarget, jobLog.getInvokeTarget())
           .between(jobLog.getCreateTime() != null && jobLog.getUpdateTime() != null,
                   CoolJobLog::getCreateTime, jobLog.getCreateTime(), jobLog.getUpdateTime());
        return lqw;
    }

    @Override
    public CoolJobLog selectJobLogById(Long jobLogId) {
        return this.getById(jobLogId);
    }

    @Override
    public void addJobLog(CoolJobLog jobLog) {
        this.save(jobLog);
    }

    @Override
    public int deleteJobLogByIds(Long[] logIds) {
        return this.removeByIds(Arrays.asList(logIds)) ? logIds.length : 0;
    }

    @Override
    public int deleteJobLogById(Long jobId) {
        return this.removeById(jobId) ? 1 : 0;
    }

    @Override
    public void cleanJobLog() {
        this.remove(new LambdaQueryWrapper<>());
    }
}